package Package1;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ShowPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Philosopher[] phis = new Philosopher[50];
	private Fork[]  forks = new Fork[50];
	private int n;
	private JTextArea testArea;
	
	
	public ShowPanel(int n, JTextArea testArea){
		super();
		this.testArea = testArea;
		init( n );
	}
	public void init(int n){
		this.n = n;
		setLayout(new CircleLayout());
		
		for(int i=0;i<n;i++)
			forks[i] = new Fork();
		
		for(int i=0;i<n;i++)
			phis[i] = new Philosopher(i+1, forks[i], forks[(i+1)%n],testArea);
		
		for(int i=0;i<n;i++){
			add(forks[i]);
			add(phis[i]);
		}
			
		for(int i=0;i<n;i++)
			phis[i].start();
		Philosopher.switchisStop(false);

	}
	
	public  void SwitchThreads(boolean turnOn){
			Philosopher.switchisStop(turnOn);
		System.out.println("enter stop");
		
	}
	public void addPhi(boolean isAdd){
		for(int i=0;i<n;i++)
			phis[i].stop();
		
		testArea.setText("");
		if(isAdd)
		n++;
		else
			if(n>2)
			n--;
		removeAll();
		init(n);
		updateUI();
	}

}
